﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Tbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using iMaker.Library;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;

namespace HKEA.CMS.Core.DAL.Tbl
{
    public class tblCust_WebLoginDAL:ItblCust_WebLogin
    {
        HKEA_CMSEntities model;
        tblCust_WebLogin weblogin;

        public tblCust_WebLoginDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static tblCust_WebLoginDAL GetInstance()
        {
            return new tblCust_WebLoginDAL();
        }

        /// <summary>
        /// 根据自增id获取 一条记录
        /// </summary>
        /// <param name="uid">自增ID</param>
        /// <returns>一条记录</returns>
        public tblCust_WebLogin GetENT(int uid)
        {
            var result = model.tblCust_WebLogin.Where(a => a.CustWebLoginUID == uid).FirstOrDefault();
            return result;
        }

        public List<tblCust_WebLogin> GetListByENT(tblCust_WebLogin ent, PagerCollection param, string strPropertyName,string strOrderBy)
        {
            var result = model.tblCust_WebLogin.Where(a => a.CustWebLoginUID > 0);
            if (ent.CustWebLoginUID > 0)
                result = result.Where(a => a.CustWebLoginUID == ent.CustWebLoginUID);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (!string.IsNullOrEmpty(ent.LoginName))
                result = result.Where(a => a.LoginName.Contains(ent.LoginName));
            if (ent.Status > 0)
                result = result.Where(a => a.Status == ent.Status);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.RecordSum = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip(param.RecordStart).Take(param.PageSize);
            param.CurRecordCount = result.Count();
            return result.ToList();
        }

        public string LoginMethod(string strUserID, string strPWD)
        {
            string strret=string.Empty;
            var ret = model.tblCust_WebLogin.Where(a => a.Status == 1).Where(a => a.LoginName == strUserID).Where(a => a.LoginPassword == strPWD).FirstOrDefault();
            if (ret != null)
            {
                ret.LastLoginDate = DateTime.Now;
                model.SaveChanges();
                tblCustomer customerENT = new tblCustomer();
                customerENT = ret.tblCustomer;
                strret = ret.CustWebLoginUID + "_" + ret.CustomerUID+ "_" + customerENT.Name_Eng + "_" + customerENT.Name_Chi ;
            }
            return strret;
        }

        public List<tblCust_WebLogin> GetListByENT(tblCust_WebLogin ent)
        {
            var result = model.tblCust_WebLogin.Where(a => a.CustWebLoginUID > 0);
            if (ent.CustWebLoginUID > 0)
                result = result.Where(a => a.CustWebLoginUID == ent.CustWebLoginUID);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (!string.IsNullOrEmpty(ent.LoginName))
                result = result.Where(a => a.LoginName.Contains(ent.LoginName));
            if (ent.Status > 0)
                result = result.Where(a => a.Status == ent.Status);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }

        public int GetCountByENT(tblCust_WebLogin ent)
        {
            var result = model.tblCust_WebLogin.Where(a => a.CustWebLoginUID > 0);
            if (ent.CustWebLoginUID > 0)
                result = result.Where(a => a.CustWebLoginUID == ent.CustWebLoginUID);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (!string.IsNullOrEmpty(ent.LoginName))
                result = result.Where(a => a.LoginName.Contains(ent.LoginName));
            if (ent.Status > 0)
                result = result.Where(a => a.Status == ent.Status);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int InsertENT(tblCust_WebLogin ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.tblCust_WebLogin.AddObject(ent);
            model.SaveChanges();
            ent = model.tblCust_WebLogin.Where(a => a.CustWebLoginUID > 0).OrderBy(a => a.CustWebLoginUID).ToList().Last();
            return ent.CustWebLoginUID;
        }

        public int UpdateENT(tblCust_WebLogin ent)
        {
            weblogin = model.tblCust_WebLogin.Where(a => a.CustWebLoginUID == ent.CustWebLoginUID).FirstOrDefault();
            if (weblogin != null)
            {
                weblogin.LastModifyBy = ent.LastModifyBy;
                weblogin.LastModifyDate = DateTime.Now;
                weblogin.CustomerUID = ent.CustomerUID;
                weblogin.LastLoginDate = ent.LastLoginDate;
                weblogin.LoginName = ent.LoginName;
                weblogin.LoginPassword = ent.LoginPassword;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(tblCust_WebLogin ent)
        {
            var temp = model.tblCust_WebLogin.Where(a => a.CustWebLoginUID > 0);
            if (ent.CustWebLoginUID > 0)
                temp = temp.Where(a=>a.CustWebLoginUID==ent.CustWebLoginUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.tblCust_WebLogin.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
    }
}
